var nowProjectIndex = -1;

function selectProject(index){
	nowProjectIndex = index;
	intoProject();
}

function intoProject() {
	if(nowProjectIndex >= 0){
		showProject(nowProjectIndex);
		selectContent(2);
	}else{
		
	}
}

function forbidRemoveProject() {
	alert("此项目禁止移除");
}

function removeProject(index) {
	var name = document.getElementById("WebProjects-context").getElementsByClassName("name")[index].value;
	if (confirm("是否删除项目\n\"" + name + "\"")) {
		var isRemoveContext = confirm("是否删除项目文件");
		rat.qure();
		var body = {};
		body.index = index;
		body.isRemoveContext = isRemoveContext;
		send("POST", "/@RemoveWebProject", true, JSON.stringify(body), showWebProjects, null);
	}
}

function addProjectByName(name) {
	var projectName = prompt("请输入项目名", name);
	if (projectName != null) {
		rat.qure();
		var body = {};
		body.projectName = projectName;
		send("POST", "/@AddWebProject", true, JSON.stringify(body), showWebProjects, null);
	}
}

function addProject() {
	send("GET", "/@AddWebProject", true, "", addProjectByName, null);
}

function showProject(index) {
	send("GET", "/@WebProjectConfig?" + index, true, "", showProjectConfig, null);
}

function showProjectConfig(text) {
	var json = JSON.parse(text);
	var index = json.index;
	var project = json.project;
	var projectConfig = json.projectConfig;
	var filtersConfig = json.filtersConfig;
	document.getElementById("projectName").value = project.name;
	document.getElementById("projectRootPath").value = project.rootPath;
	document.getElementById("projectUrl").value = projectConfig.url;
	var configs = document.getElementsByClassName("projectConfig")[0].children;
	var filter = null;
	for (let i = 0; i < json.filtersConfig.length; i++) {
		if (json.filtersConfig[i].filterName == "ResourceServer") {
			filter = json.filtersConfig[i];
			break;
		}
	}
	if (filter != null) {
		configs[1].children[1].style.opacity = "1";
		document.getElementById("resourceRootPath").value = filter.config.rootPath;
		document.getElementById("indexPath").value = filter.config.indexPath;
		document.getElementById("fileReadLimit").value = filter.config.fileReadLimit;
	} else {
		configs[1].children[1].style.opacity = "0.5";
		document.getElementById("resourceRootPath").value = "";
		document.getElementById("indexPath").value = "";
		document.getElementById("fileReadLimit").value = "";
	}
	filter = null;
	for (let i = 0; i < json.filtersConfig.length; i++) {
		if (json.filtersConfig[i].filterName == "SessionServer") {
			filter = json.filtersConfig[i];
			break;
		}
	}
	if (filter != null) {
		configs[2].children[1].style.opacity = "1";
		document.getElementById("SESSION_ID").value = filter.config.SESSION_ID;
		document.getElementById("maxLiveTime").value = filter.config.maxLiveTime;
		document.getElementById("checkTime").value = filter.config.checkTime;
		document.getElementById("SessionListButton").onclick = intoSessionList;
	} else {
		configs[2].children[1].style.opacity = "0.5";
		document.getElementById("SESSION_ID").value = "";
		document.getElementById("maxLiveTime").value = "";
		document.getElementById("checkTime").value = "";
		document.getElementById("SessionListButton").onclick = cantIntoSessionList;
	}
	filter = null;
	for (let i = 0; i < json.filtersConfig.length; i++) {
		if (json.filtersConfig[i].filterName == "ProxyServer") {
			filter = json.filtersConfig[i];
			break;
		}
	}
	if (filter != null) {
		configs[3].children[1].style.opacity = "1";
		document.getElementById("proxyCharset").value = filter.config.charset;
		document.getElementById("proxyReceiveTimeOut").value = filter.config.receiveTimeOut;
		document.getElementById("continueTimeOut").value = filter.config.continueTimeOut;
		document.getElementById("ProxyListButton").onclick = intoProxyList;
	} else {
		configs[3].children[1].style.opacity = "0.2";
		document.getElementById("proxyCharset").value = "";
		document.getElementById("proxyReceiveTimeOut").value = "";
		document.getElementById("continueTimeOut").value = "";
		document.getElementById("ProxyListButton").onclick = cantIntoProxyList;
	}
	showLibPath(JSON.stringify([index, projectConfig.libPath]));
	showClassPath(JSON.stringify([index, projectConfig.classPath]));
	document.getElementById("updateProjectConfig").innerHTML =
		"<button type=\"button\" onclick=\"updateProjectConfig(" + index + ")\">更改</button>";
}

function showLibPath(jsonText) {
	var json = JSON.parse(jsonText);
	var index = json[0];
	var libPath = json[1];
	var text = "";
	if (libPath != undefined) {
		for (let i = 0; i < libPath.length; i++) {
			if (libPath.length == 1 && libPath[i] == undefined) {
				break;
			}
			text += "<input type=\"button\" name=\"libPath\" value=\"";
			text += libPath[i];
			text += "\" />";
			text += "<button type=\"button\" class=\"remove\" onclick=\"removeLibPath(" + index + "," + i +
				")\">删</button>";
			text += "<br>";
		}
	}
	text += "<button type=\"button\" class=\"add\" onclick=\"addLibPath(" + index + ")\" >添加</button>";
	document.getElementById("libPathList").innerHTML = text;
}

function removeLibPath(projectIndex, libIndex) {
	if (confirm("是否删除库路径\n" + "\"" + document.getElementById("libPathList").getElementsByTagName("input")[libIndex]
			.value + "\"")) {
		var isRemoveContext = confirm("是否删除文件");
		rat.qure();
		var json = {
			projectIndex: projectIndex,
			libIndex: libIndex,
			isRemoveContext: isRemoveContext
		};
		send("POST", "/@RemoveLibPath", true, JSON.stringify(json), showLibPath, null);
	}
}

function addLibPath(projectIndex) {
	var libPath = prompt("请输入要增加的库路径");
	if (libPath != null) {
		rat.qure();
		var body = {};
		body.projectIndex = projectIndex;
		body.libPath = libPath;
		send("POST", "/@AddLibPath", true, JSON.stringify(body), showLibPath, null);
	}
}

function showClassPath(jsonText) {
	var json = JSON.parse(jsonText);
	var index = json[0];
	var classPath = json[1];
	var text = "";
	if (classPath != undefined) {
		for (let i = 0; i < classPath.length; i++) {
			text += "<input type=\"button\" name=\"calssPath\" value=\"";
			text += classPath[i];
			text += "\" />";
			text += "<button type=\"button\" class=\"remove\" onclick=\"removeClassPath(" + index + "," + i +
				")\">删</button>";
			text += "<br>";
		}
	}
	text += "<button type=\"button\" class=\"add\" onclick=\"addClassPath(" + index + ")\">添加</button>";
	document.getElementById("classPathList").innerHTML = text;
}

function removeClassPath(projectIndex, classIndex) {
	if (confirm("是否删除类路径\n" + "\"" + document.getElementById("classPathList").getElementsByTagName("input")[classIndex]
			.value + "\"")) {
		var isRemoveContext = confirm("是否删除文件");
		rat.qure();
		var json = {
			projectIndex: projectIndex,
			classIndex: classIndex,
			isRemoveContext: isRemoveContext
		};
		send("POST", "/@RemoveClassPath", true, JSON.stringify(json), showClassPath, null);
	}
}

function addClassPath(projectIndex) {
	var classPath = prompt("请输入要增加的类路径");
	if (classPath != null) {
		rat.qure();
		var body = {};
		body.projectIndex = projectIndex;
		body.classPath = classPath;
		send("POST", "/@AddClassPath", true, JSON.stringify(body), showClassPath, null);
	}
}

function updateProjectConfig(index) {
	rat.qure();
	var json = {};
	json.index = index;
	json.project = {};
	json.projectConfig = {};
	json.filtersConfig = {};
	json.project.name = document.getElementById("projectName").value;
	json.project.rootPath = document.getElementById("projectRootPath").value;
	json.projectConfig.url = document.getElementById("projectUrl").value;
	json.filtersConfig.resourceConfig = {};
	json.filtersConfig.resourceConfig.rootPath = document.getElementById("resourceRootPath").value;
	json.filtersConfig.resourceConfig.indexPath = document.getElementById("indexPath").value;
	json.filtersConfig.resourceConfig.fileReadLimit = document.getElementById("fileReadLimit").value;
	json.filtersConfig.sessionConfig = {};
	json.filtersConfig.sessionConfig.SESSION_ID = document.getElementById("SESSION_ID").value;
	json.filtersConfig.sessionConfig.maxLiveTime = document.getElementById("maxLiveTime").value;
	json.filtersConfig.sessionConfig.checkTime = document.getElementById("checkTime").value;
	json.filtersConfig.proxyConfig = {};
	json.filtersConfig.proxyConfig.proxyCharset = document.getElementById("proxyCharset").value;
	json.filtersConfig.proxyConfig.proxyReceiveTimeOut = document.getElementById("proxyReceiveTimeOut").value;
	json.filtersConfig.proxyConfig.continueTimeOut = document.getElementById("continueTimeOut").value;
	send("POST", "/@WebProjectConfig", true, JSON.stringify(json), showProjectConfig, null);
}
